﻿using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace UnInstallTicketM
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string AppRoot = Path.GetPathRoot(System.Environment.SystemDirectory) + "Program Files\\SSGS\\TicketManager";
                //stop service
                ServiceController service = new ServiceController("TicketProcessingService");
                service.Stop();
                Logger.CreateLogEntry("Service stopped");
                // unistall Windows Service
                string path = AppRoot + @"\TicketM\Configuration\TicketProcessingService.exe";
                ManagedInstallerClass.InstallHelper(new string[] { "/u", path });
                Logger.CreateLogEntry("Service uninstalled");
                //remove web site
                ServerManager serverMgr = new ServerManager();
                Site site = serverMgr.Sites["TicketManager"];
                serverMgr.Sites.Remove(site);
                serverMgr.CommitChanges();
                Logger.CreateLogEntry("Site removed");
                //remove app pool
                ApplicationPool appPool = serverMgr.ApplicationPools["TicketPool"];
                serverMgr.ApplicationPools.Remove(appPool);
                serverMgr.CommitChanges();
                Logger.CreateLogEntry("App pool removed");
                //delete folder SSGS
                string folder = Path.GetPathRoot(System.Environment.SystemDirectory) + "Program Files\\SSGS";
                Directory.Delete(folder, true);
                Logger.CreateLogEntry("folder SSGS under program files deleted");
                //remove event handlers
                string file1 = Path.GetPathRoot(System.Environment.SystemDirectory) + @"\Program Files\Microsoft Team Foundation Server 11.0\Application Tier\Web Services\bin\Plugins\TicketNotification.dll";
                string file2 = Path.GetPathRoot(System.Environment.SystemDirectory) + @"\Program Files\Microsoft Team Foundation Server 11.0\Application Tier\Web Services\bin\Plugins\TicketNotification.dll.config";
                File.Delete(file1);
                File.Delete(file2);
                Logger.CreateLogEntry("Files from plugins deleted. Event handlers");
                Console.WriteLine("Ticket Manager System uninstalled now... Press any key");
                Console.ReadLine();

            }
            catch (Exception ex)
            {
                string logEntry = ex.Message;
                if (ex.InnerException != null)
                {
                    logEntry += " " + ex.InnerException.Message;
                }
                Logger.CreateLogEntry(logEntry);

            }
        }
    }
}
